package org.vectrics.recruiter.struts;

import javax.servlet.ServletException;

import org.apache.log4j.Logger;
import org.vectrics.DataException;
import org.vectrics.common.reporting.ReportManager;
import org.vectrics.dataweb.WebServiceAction;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.ApplicationContext;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.BeanLocator;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitStart;
import org.vectrics.recruiter.config.RecruitConfig;
import org.vectrics.recruiter.document.DocumentRequestManager;
import org.vectrics.recruiter.reporting.RecruitReportManager;
import org.vectrics.struts.DocumentResponse;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsServlet;
import org.vectrics.system.PagePrivilege;
import org.vectrics.user.Person;


public abstract class RecruiterServlet extends VectricsServlet {
	public static final long serialVersionUID = 11327123583784L;
    private transient static Logger log = Logger.getLogger(RecruiterServlet.class);

    @Override
	protected final WebServiceAction getConfiguration(String command) {
    	WebServiceAction resultado = (WebServiceAction) BeanLocator.findBean(command);
		if (resultado==null) {
			resultado = new HostedSiteWebService();
		}
	    
		return resultado;
	}
    
	/**
	 * 
	 */
    @Override
	protected boolean isReportRequest(Request request) {
		boolean result = false;
		
		String uri = request.getRequestURI();
		
		if ((uri.indexOf("/report") > 0) || (uri.startsWith("report/"))) {
			result = true;
		}
		if (log.isDebugEnabled())
			log.debug("isReportRequest() - result: " + result + "  / URI = " + uri);
		return result;
    	
    }
    
    @Override
	protected boolean isDocumentRequest(Request request) {
		boolean result = false;
		
		String uri = request.getRequestURI();
		
		if (uri.indexOf("retrievedocument") > 0) {
			result = true;
		}
		if (log.isDebugEnabled())
			log.debug("isDocumentRequest() - result: " + result + "  / URI = " + uri);
		return result;
	}

    @Override
	protected void processDocumentRequest(Request request, 
				DocumentResponse documentResponse, Person authPerson) {
		log.debug("processDocumentRequest() - return document requested by user");
		//String resumeId = request.getParameter("resumeId");
		//if ((resumeId != null) && (resumeId.trim().length() > 0)) {
			DocumentRequestManager resumeRequestManager = new DocumentRequestManager();
			resumeRequestManager.service(request, documentResponse, authPerson);
		//}
	}
	
    @Override
    protected boolean isDataRequest(Request request) {
    	log.debug("Requested URI: " + request.getRequestURI());
    	if (request.getRequestURI().endsWith("/ws")) {
    		log.debug("Request is a web service call");
    		return true;
    	}
    	return(false);
    }
    
    @Override
    protected boolean isProtected(Request request) {
    	PagePrivilege pagePriv = CoreServiceLocator.getUserAuthService().findRelevantPagePrivilege(request);
    	return(!(pagePriv == null));
    }

    @Override
    protected boolean isEncrypted(Request request) {
    	return(false);
    }

    public boolean userHasAccess(Request request, Person authPerson, Domain domain) {
    	boolean result = CoreServiceLocator.getUserAuthService().doesUserHavePagePrivilege(request, authPerson, domain);
    	return(result);
    }
    
    
    @Override
    protected String getLoginUri(Request request) {
    	if (request.getRequestURI().startsWith("/hrs-intranet/")) {
    		return("/hrs-intranet/login.do");
    	} else {
    		return("/hrs-public/util/login.do");
    	}
    }
    
    
    /**Initialize global variables*/
    @Override
    public final ApplicationContext initialize() throws ServletException {
        log.info("RecruiterServlet initializing.");
		RecruitStart starter = RecruitStart.getInstance();
        return starter.initializeWebApplication(this.getServletContext());
    }

    /**Initialize quartz schedulers */
    @Override
    public final void initializeQuartz() throws ServletException {
        log.info("RecruiterServlet initializing.");
		RecruitStart starter = RecruitStart.getInstance();
        starter.startBatchJobsScheduling(ThreadProperties.getApplicationContext());
    }
    
    @Override
    protected ReportManager getReportManager() throws DataException {
    	String dataSourceJndi = RecruitConfig.getRecruitInstance().getDataSourceJndi();
    	
		if (dataSourceJndi == null) {
			throw new DataException("The 'dataSourceAttribute' was not found in the reporting section of the config file");
		}
		else {
			log.debug("DataSource: " + dataSourceJndi);
		}
    	return(new RecruitReportManager(dataSourceJndi));
    }
    
    
}